package cfig

import cfig.toolset.AdbTool
import org.slf4j.LoggerFactory
import se.vidstige.jadb.JadbConnection
import se.vidstige.jadb.JadbDevice
import cfig.toolset.BootCtl

@ExperimentalStdlibApi
class DeviceInfo {
    private val log = LoggerFactory.getLogger(DeviceInfo::class.java)
    val jadb = JadbConnection()
    var dut: JadbDevice
    var bootCtl: BootCtl

    constructor() {
        if (jadb.devices.size != 1) {
            log.error("Must have only 1 device connected")
        }
        dut = jadb.devices[0]
        bootCtl = BootCtl(dut)
        assert(dut.state == JadbDevice.State.Device)
    }

    fun doSth() {
        val slotInfo = dut.executeShell("bootctl get-current-slot").readBytes().decodeToString()
        log.info("Slot: [" + slotInfo.trim() + "]")
        val useAB = AdbTool().getProp(dut, "getprop ro.build.ab_update") == "true"
        log.info("AB: $useAB")
        println("Product: " + AdbTool().getProp(dut, "ro.product.name"))
    }

    override fun toString(): String {
        return "DeviceInfo(jadb=$jadb, dut=$dut, bootCtl=$bootCtl)"
    }
}
